package com.danger.Activity.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.danger.Activity.Bean.Course;
import com.danger.Activity.R;
import com.danger.Activity.Util.ImageLoaderUtil;
import com.nostra13.universalimageloader.core.assist.ImageSize;

import java.util.List;

/**
 * Created by 俊 on 2016/5/14.
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private List<Course> list;
    private Context context;
    private LayoutInflater inflater;

    public MyAdapter(List<Course> list, Context context) {
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.recycleview_item, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        new ImageLoaderUtil(new ImageSize(64, 64), holder.recycleView_imageView, list.get(position).getPicSmall(), context).load();
        holder.recycleView_title_textView.setText(list.get(position).getName());
        holder.recycleView__content_textView.setText(list.get(position).getDescription());

        // 如果设置了回调，则设置点击事件
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        public ImageView recycleView_imageView;
        public TextView recycleView_title_textView;
        public TextView recycleView__content_textView;

        public MyViewHolder(View itemView) {
            super(itemView);
            recycleView_imageView = (ImageView) itemView.findViewById(R.id.recycleView_imageView);
            recycleView_title_textView = (TextView) itemView.findViewById(R.id.recycleView_title_textView);
            recycleView__content_textView = (TextView) itemView.findViewById(R.id.recycleView__content_textView);
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }


}
